package swing;

import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import org.apache.ibatis.session.SqlSession;

import util.GetSqlSession;
import util.IPAddress;
import util.LogUtil;
import util.SwingElement;
import util.UnicodeReader;
import auto.vpn.Vpn;
import baseDao.LexiconDao;
import baseDao.TaskListDao;
import baseDao.WebsiteDao;
import entity.Lexicon;
import entity.TaskList;
import entity.User;
import entity.Website;

public class TackTo extends JDialog {
	private Date datewbk;
	private Date date2wbk;
	private static User user = Login.getUser();
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel jp1;// 账户信息面板
	private JPanel jp2;// 账户信息面板
	// jp1

	private JLabel jl1;// 账户信息
	private JLabel jl2;// 账户
	private JLabel jl3;// 账户id
	private JLabel jl4;// 任务时间
	private JLabel jl5;// -
	private JLabel jl6;// -
	private JLabel jl7;// :
	private JLabel jl8;// 开始时间
	private JLabel jl9;// :
	private JLabel jl10;// 结束时间
	private JLabel jl11;// :
	private JLabel jl12;// 任务间隔
	private JLabel jl13;// :
	private JLabel jl14;// )分钟随机
	private JLabel jl15;// 计划安排天数:
	// jp2
	private JLabel jp2_jl1;// (s1):
	private JLabel jp2_jl2;// 或用
	private JLabel jp2_jl3;// --对应
	private JLabel jp2_jl4;// 上下浏览到底
	private JLabel jp2_jl5;// ~
	private JLabel jp2_jl6;// ------------
	private JLabel jp2_jl7;// 随机选词
	private JLabel jp2_jl8;// ------------
	private JLabel jp2_jl9;// 特色功能
	private JLabel jp2_jl10;// 改词不能选词
	private JLabel jp2_jl11;// (秒)

	// jp1
	private ButtonGroup bg1 = new ButtonGroup();// 以下按钮
	private JRadioButton jrb1;// 单时间按钮
	private JRadioButton jrb2;// 多时间按钮
	private ButtonGroup bg2 = new ButtonGroup();// 以下按钮
	private JRadioButton jrb3;// 今天 开始
	private JRadioButton jrb4;// 明天开始
	private ButtonGroup bg3 = new ButtonGroup();// 以下按钮
	private JRadioButton jrb5;// ip
	private JRadioButton jrb6;//
	private JRadioButton jrb7;//
	// jp2
	private ButtonGroup jp2_bg1 = new ButtonGroup();// 以下按钮
	private JRadioButton jp2_jrb2;// 搜索进站
	private JRadioButton jp2_jrb3;// 百度进入
	private JRadioButton jp2_jrb5;// 360进入
	private ButtonGroup jp2_bg2 = new ButtonGroup();// 以下按钮
	private JRadioButton jp2_jrb1;// 直接进站
	private JRadioButton jp2_jrb4;// 特色进站

	// jp1
	private JTextField jf1;// jp1年份
	private JTextField jf2;// jp1月
	private JTextField jf3;// jp1日
	private JTextField jf4;// jp1时
	private JTextField jf5;// jp1分
	private JTextField jf6;// jp1开始时间 时
	private JTextField jf7;// jp1开始时间 分
	private JTextField jf8;// jp1结束时间 时
	private JTextField jf9;// jp1结束时间 分
	private JTextField jf10;// jp1任务间隔 分
	private JTextField jf11;// jp1任务间隔 分随机
	private JTextField jf12;// jp1计划安排天数
	// jp2
	private JTextField jp2_jf1;// jp2网址
	private JTextField jp2_jf2;// jp2~前框
	private JTextField jp2_jf3;// jp2~后框
	private JTextField jp2_jf4;// jp1特色选词

	// jp2
	private JButton jp2_jb1;// 网址库
	private JButton jp2_jb2;// 指定改词
	private JCheckBox jp2_jc1;// 是否改词
	private JButton jp2_jb3;// 词库1
	private JButton jp2_jb4;// 搜索词库
	private JButton jp2_jb5;// 热门网址

	private JPanel jp3;
	private JLabel jp3_j1;// 随机打开子页面数：
	private JLabel jp3_j2;// ~
	private JLabel jp3_j3;// （pv数）
	private JLabel jp3_j4;// 哪个页面点广告：

	private ButtonGroup jp3_bg1 = new ButtonGroup();// 以下按钮
	private JRadioButton jp3_jrb1;// 单入口页面点(即左侧S1设置的网址页面)
	private JRadioButton jp3_jrb2;// 单以下内页随机取一个（入口页面可以找到该链接）

	private JTextField jp3_jf6;// 页面数第一个文本框
	private JTextField jp3_jf7;// 页面数第二个文本框
	private JTextField jp3_jf1;// 第一个链接文本框
	private JTextField jp3_jf2;// 第二个链接文本框
	private JTextField jp3_jf3;// 第三个链接文本框
	private JTextField jp3_jf4;// 第四个链接文本框
	private JTextField jp3_jf5;// 第五个链接文本框

	private JPanel jp4;
	private JLabel jp4_j1;// 广告联盟：
	private JLabel jp4_j2;// 商业推广链接点击个数：
	private JLabel jp4_j3;// ~
	private JLabel jp4_j4;// (随机)
	private JLabel jp4_j5;// 搜索结果快照点击个数
	private JLabel jp4_j6;// ~
	private JLabel jp4_j7;// (随机)
	private JLabel jp4_j8;// 浏览器关闭前停留时间：

	private JCheckBox jp4_jcb1;// 开启选词功能(不兼容所有广告块,要用课咨询)
	private JRadioButton jp4_jrb1;// 360新代码
	private JButton jp4_jb1;// 词库凌
	private JButton jp4_jb2;// 词库早
	private JButton jp4_jb3;// 词库晚
	private JButton jp4_jb4;// 词库中
	private JButton jp4_jb5;// 任务传入

	private JTextField jp4_jf1;// 商业推广第一个
	private JTextField jp4_jf2;// 商业推广第二个
	private JTextField jp4_jf3;// 搜索结果第一个
	private JTextField jp4_jf4;// 搜索结果第二个
	private JTextField jp4_jf5;// 浏览器关闭第一个
	private JTextField jp4_jf6;// 浏览器关闭第二个

	TackTo(JFrame aqa) {
		super(aqa, true);
		this.setTitle("任务创建");
		this.setSize(670, 320);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		Container c = getContentPane();
		c.setLayout(null);
		c.setVisible(true);
		// 面板区
		jp1 = SwingElement.getJP("");
		jp1.setLayout(null);
		jp1.setBounds(3, 3, 329, 300);
		jp2 = SwingElement.getJP("");
		jp2.setLayout(null);
		jp2.setBounds(333, 3, 329, 300);

		// 文本标签区
		// jp1
		jl1 = SwingElement.getJL("账户信息");
		jl1.setBounds(1, 0, 80, 20);
		jl2 = SwingElement.getJL("账户:");
		jl2.setBounds(1, 20, 80, 20);
		jl3 = SwingElement.getJL(String.valueOf(user.getId()));
		jl3.setBounds(100, 20, 100, 20);
		jl4 = SwingElement.getJL("任务时间");
		jl4.setBounds(20, 60, 60, 20);
		jl5 = SwingElement.getJL("-");
		jl5.setBounds(131, 60, 9, 20);
		jl6 = SwingElement.getJL("-");
		jl6.setBounds(162, 60, 9, 20);
		jl7 = SwingElement.getJL(":");
		jl7.setBounds(222, 60, 9, 20);
		jl8 = SwingElement.getJL("开始时间:");
		jl8.setBounds(30, 100, 70, 20);
		jl9 = SwingElement.getJL(":");
		jl9.setBounds(160, 100, 9, 20);
		jl10 = SwingElement.getJL("结束时间:");
		jl10.setBounds(30, 120, 70, 20);
		jl11 = SwingElement.getJL(":");
		jl11.setBounds(160, 120, 9, 20);
		jl12 = SwingElement.getJL("任务间隔:");
		jl12.setBounds(30, 140, 70, 20);
		jl13 = SwingElement.getJL("+(0-");
		jl13.setBounds(160, 140, 40, 20);
		jl14 = SwingElement.getJL(")分钟随机");
		jl14.setBounds(210, 140, 80, 20);
		jl15 = SwingElement.getJL("计划安排天数:");
		jl15.setBounds(30, 180, 100, 20);
		// jp2
		jp2_jl1 = SwingElement.getJL("(S1)你的网址: (带http://的完整地址)");
		jp2_jl1.setBounds(1, 0, 280, 20);
		jp2_jl2 = SwingElement.getJL("或用");
		jp2_jl2.setBounds(1, 50, 30, 20);
		jp2_jl3 = SwingElement.getJL("-对应-");
		jp2_jl3.setBounds(146, 50, 44, 20);
		jp2_jl4 = SwingElement.getJL("上下浏览到底之后再停留时间:");
		jp2_jl4.setBounds(0, 80, 180, 20);
		jp2_jl5 = SwingElement.getJL("~");
		jp2_jl5.setBounds(205, 80, 10, 20);
		jp2_jl6 = SwingElement.getJL("----------------------------------------------");
		jp2_jl6.setBounds(1, 100, 290, 20);
		jp2_jl7 = SwingElement.getJL("随机选词(排名需在前6页)");
		jp2_jl7.setBounds(115, 160, 200, 20);
		jp2_jl8 = SwingElement.getJL("----------------------------------------------");
		jp2_jl8.setBounds(1, 220, 290, 20);
		jp2_jl9 = SwingElement.getJL("特色功能参数输入框(改词:change_words)");
		jp2_jl9.setBounds(1, 240, 300, 20);
		jp2_jl10 = SwingElement.getJL("改词则不能选词,词库则共用");
		jp2_jl10.setBounds(1, 260, 300, 20);
		jp2_jl11 = SwingElement.getJL("(秒)");
		jp2_jl11.setBounds(241, 80, 50, 20);

		// 单选按钮区
		// jp1
		jrb1 = new JRadioButton("单时间点任务安排", true);
		jrb1.setBounds(1, 40, 150, 20);
		jrb2 = new JRadioButton("多时间点任务安排");
		jrb2.setBounds(1, 80, 150, 20);
		jrb3 = new JRadioButton("今天开始", true);
		jrb3.setBounds(30, 160, 80, 20);
		jrb4 = new JRadioButton("明天开始");
		jrb4.setBounds(130, 160, 80, 20);
		jrb5 = new JRadioButton("ADSL拨号(将宽带连接重命名为\"adsl\")");
		jrb5.setBounds(1, 200, 280, 20);
		jrb6 = new JRadioButton("VPN连接(如:ip直连客户端  勾选\"断开连接\")", true);
		jrb6.setBounds(1, 220, 280, 20);
		jrb7 = new JRadioButton("不切换IP(软件测试用)");
		jrb7.setBounds(1, 240, 280, 20);
		bg1.add(jrb1);
		bg1.add(jrb2);
		bg2.add(jrb3);
		bg2.add(jrb4);
		bg3.add(jrb5);
		bg3.add(jrb6);
		bg3.add(jrb7);
		// jp2
		jp2_jrb1 = new JRadioButton("直接进站");
		jp2_jrb1.setBounds(1, 120, 200, 20);
		jp2_jrb2 = new JRadioButton("搜索进站");
		jp2_jrb2.setBounds(1, 140, 100, 20);
		jp2_jrb3 = new JRadioButton("百度进入");
		jp2_jrb3.setBounds(30, 130, 100, 20);
		jp2_jrb5 = new JRadioButton("360进入",true);
		jp2_jrb5.setBounds(160, 130, 100, 20);
		jp2_jrb4 = new JRadioButton("特色进站(百度搜索带词+热门网址来源)");
		jp2_jrb4.setBounds(1, 180, 250, 20);
		jp2_bg2.add(jp2_jrb1);
		jp2_bg2.add(jp2_jrb2);
		jp2_bg2.add(jp2_jrb4);
		jp2_bg2.add(jp2_jrb3);
		jp2_bg2.add(jp2_jrb5);
		// 文本框区
		// jp1
		datewbk = new Date();
		jf1 = SwingElement.getJT("", 90, 60, 40, 20);
		jf1.setText((datewbk.getYear() + 1900) + "");
		jf2 = SwingElement.getJT("", 141, 60, 21, 20);
		jf2.setText((datewbk.getMonth() + 1) + "");
		jf3 = SwingElement.getJT("", 172, 60, 21, 20);
		jf3.setText(datewbk.getDate() + "");
		jf4 = SwingElement.getJT("", 200, 60, 21, 20);
		jf4.setText(datewbk.getHours() + "");
		jf5 = SwingElement.getJT("", 232, 60, 21, 20);
		jf5.setText(datewbk.getMinutes() + "");
		jf6 = SwingElement.getJT("", 130, 100, 30, 20);
		jf6.setText(datewbk.getHours() + "");
		jf7 = SwingElement.getJT("", 170, 100, 30, 20);
		jf7.setText(datewbk.getMinutes() + "");

		jf8 = SwingElement.getJT("", 130, 120, 30, 20);
		long time = datewbk.getTime();
		long a = 60 * 60 * 2 * 1000 + time;
		date2wbk = new Date(a);
		jf8.setText("" + date2wbk.getHours());
		jf9 = SwingElement.getJT("", 170, 120, 30, 20);
		jf9.setText("" + date2wbk.getMinutes());
		jf10 = SwingElement.getJT("30", 130, 140, 30, 20);

		jf11 = SwingElement.getJT("5", 195, 140, 15, 20);
		jf12 = SwingElement.getJT("1", 130, 180, 30, 20);
		// jp2
		jp2_jf1 = SwingElement.getJT("", 1, 20, 250, 20);
		jp2_jf2 = SwingElement.getJT("", 180, 80, 25, 20);
		jp2_jf3 = SwingElement.getJT("", 216, 80, 25, 20);
		jp2_jf4 = SwingElement.getJT("", 1, 240, 250, 20);

		// 按钮区
		// jp2
		jp2_jb1 = SwingElement.getJB("网址库数据库");
		jp2_jb1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// main1 text = new main1();
				WebsiteSwing text = new WebsiteSwing(TackTo.this);
				text.setVisible(true);
			}
		});
		jp2_jb1.setBounds(30, 50, 115, 20);
		jp2_jb2 = SwingElement.getJB("改词数据库");
		jp2_jb2.setBounds(200, 50, 100, 20);
		jp2_jb2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// main1 text = new main1();
				LexiconSwing text = new LexiconSwing(TackTo.this);
				text.setVisible(true);
			}
		});
		jp2_jc1 = new JCheckBox();
		jp2_jc1.setBounds(180, 50, 20, 20);
		jp2_jb3 = SwingElement.getJB("网址库记事本");
		jp2_jb3.setBounds(30, 80, 115, 20);
		jp2_jb3.addActionListener(new ActionListener() {// 网址库
			@Override
			public void actionPerformed(ActionEvent e) {
				SqlSession session = GetSqlSession.getSession();
				OutputStreamWriter output = null;
				try {
					WebsiteDao dao = session.getMapper(WebsiteDao.class);
					List<Website> list = dao.selectAll();
					session.close();
					StringBuffer sb = new StringBuffer(); // 原有txt内容
					try {
						for (int i = 0; i < list.size(); i++) {
							// 循环拼网址库
							if(list.size()-1==i){
								sb.append(list.get(i).getWebsite() );
								break;
							}
							sb.append(list.get(i).getWebsite() + "\r\n");
						}
						File file = new File(System.getProperty("user.dir")
								+ "\\notepad\\webSite.txt");
						System.out.println(sb.length()+"   sb.length");
						if (!file.exists()) {// 没有文件
							JOptionPane.showMessageDialog(null, "缺少程序依赖文件,请重新安装程序");
							return;
						} 
						if (IPAddress.codeString(file).equalsIgnoreCase("GBK")) {
							System.out.println("GBK");
							output = new OutputStreamWriter(
									new FileOutputStream(file),"GBK");
						} else {
							System.out.println("UTF-8");
							output = new OutputStreamWriter(
									new FileOutputStream(file),"UTF-8");
						}
							// 写入lex.txt
							output.write(sb.toString());
							output.close();
					} catch (Exception e1) {
						e1.printStackTrace();
					}
					session = GetSqlSession.getSession();
					dao = session.getMapper(WebsiteDao.class);
					dao.deleteAll();
					session.close();
					// 获得当前工程路径,打开指定txt文件
					Vpn.executeCmd("notepad " + System.getProperty("user.dir")
							+ "\\notepad\\webSite.txt");
					File file = new File(System.getProperty("user.dir")
							+ "\\notepad\\webSite.txt");

					String text = txt2String(file);
					String[] split = text.split(",");
					Website w = new Website();
					session = GetSqlSession.getSession();
					dao = session.getMapper(WebsiteDao.class);
					for (int i = 0; i < split.length; i++) {
						w.setWebsite(split[i]);
						dao.add(w);
					}
					session.close();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		jp2_jb4 = SwingElement.getJB("词库记事本");
		jp2_jb4.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				SqlSession session = GetSqlSession.getSession();
				try {
					LexiconDao dao = session.getMapper(LexiconDao.class);
					List<Lexicon> list = dao.selectAll();
					StringBuffer sb = new StringBuffer(); // 原有txt内容
					try {
						for (int i = 0; i < list.size(); i++) {
							// 循环拼接词库
							sb.append(list.get(i).getWords() + "\r\n");
						}
						File file = new File(System.getProperty("user.dir")
								+ "\\notepad\\lex.txt");
						if (!file.exists()) {// 没有文件
							JOptionPane.showMessageDialog(null, "文件被非法删除,请使用数据\n库添加或重新安装软件");
							return;						
						} 
						OutputStreamWriter output = null;
						if (IPAddress.codeString(file).equalsIgnoreCase("GBK")) {
							System.out.println("GBK");
							output = new OutputStreamWriter(
									new FileOutputStream(file),"GBK");
						} else {
							System.out.println("UTF-8");
							output = new OutputStreamWriter(
									new FileOutputStream(file),"UTF-8");
						}
						// 写入lex.txt
						output.write(sb.toString());
						output.close();
						session.close();
					} catch (Exception e1) {
						e1.printStackTrace();
					}
					// 获得当前工程路径,打开指定txt文件
					session = GetSqlSession.getSession();
					dao = session.getMapper(LexiconDao.class);
					dao.deleteAll();
					session.close();
					Vpn.executeCmd("notepad " + System.getProperty("user.dir")
							+ "\\notepad\\lex");
					File file = new File(System.getProperty("user.dir")
							+ "\\notepad\\lex.txt");
					String text = txt2String(file);
					String[] split = text.split(",");
					Lexicon le = new Lexicon();
					session = GetSqlSession.getSession();
					dao = session.getMapper(LexiconDao.class);
					for (int i = 0; i < split.length; i++) {
						le.setWords(split[i]);
						dao.add(le);
					}
					session.close();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		jp2_jb4.setBounds(200, 80, 100, 20);
		jp2_jb5 = SwingElement.getJB("热门网址");
		jp2_jb5.setBounds(130, 180, 100, 20);
		//
		jp1.add(jl1);
		jp1.add(jl2);
		jp1.add(jl3);
		jp1.add(jl4);
		jp1.add(jl5);
		jp1.add(jl6);
		jp1.add(jl7);
		jp1.add(jl8);
		jp1.add(jl9);
		jp1.add(jl10);
		jp1.add(jl11);
		jp1.add(jl12);
		jp1.add(jl13);
		jp1.add(jl14);
		jp1.add(jl15);

		jp1.add(jrb1);
		jp1.add(jrb2);
		jp1.add(jrb3);
		jp1.add(jrb4);
		jp1.add(jrb5);
		jp1.add(jrb6);
		jp1.add(jrb7);

		jp1.add(jf1);
		jp1.add(jf2);
		jp1.add(jf3);
		jp1.add(jf4);
		jp1.add(jf5);
		jp1.add(jf6);
		jp1.add(jf7);
		jp1.add(jf8);
		jp1.add(jf9);
		jp1.add(jf10);
		jp1.add(jf11);
		jp1.add(jf12);

		// getJLB
		jp2.add(jp2_jl1);
		jp2.add(jp2_jl2);
		jp2.add(jp2_jl3);
//		jp2.add(jp2_jl4);
//		jp2.add(jp2_jl5);
		jp2.add(jp2_jl6);
		// jp2.add(jp2_jl7);
		// jp2.add(jp2_jl8);
		// jp2.add(jp2_jl9);
		// jp2.add(jp2_jl10);
//		jp2.add(jp2_jl11);
		// getJT
		jp2.add(jp2_jf1);
//		jp2.add(jp2_jf2);
//		jp2.add(jp2_jf3);
		// jp2.add(jp2_jf4);
		// 按钮
		jp2.add(jp2_jb1);
		jp2.add(jp2_jb2);
		jp2.add(jp2_jb3);
		jp2.add(jp2_jc1);
		jp2.add(jp2_jb4);
		// jp2.add(jp2_jb5);
		// 单选框
		//jp2.add(jp2_jrb1);
		//jp2.add(jp2_jrb2);
		jp2.add(jp2_jrb3);
		// jp2.add(jp2_jrb4);
		jp2.add(jp2_jrb5);

		c.add(jp1);
		c.add(jp2);

		jp3 = SwingElement.getJP("");
		jp3.setLayout(null);
		jp3.setBounds(3, 300, 329, 300);

		// 文本getJLB区
		jp3_j1 = SwingElement.getJL("随机打开子页面数：");
		jp3_j1.setBounds(1, 160, 150, 20);
		jp2.add(jp3_j1);
		jp3_j2 = SwingElement.getJL("~");
		jp3_j2.setBounds(147, 160, 10, 15);
		jp2.add(jp3_j2);
		jp3_j3 = SwingElement.getJL("(pv数)");
		jp3_j3.setBounds(178, 160, 120, 15);
		jp2.add(jp3_j3);
		jp3_j4 = SwingElement.getJL("哪个页面点广告:");
		jp3_j4.setBounds(5, 40, 120, 15);

		// getJT区
		jp3_jf6 = SwingElement.getJT("1", 128, 160, 18, 20);
		jp3_jf7 = SwingElement.getJT("3", 158, 160, 18, 20);
		jp2.add(jp3_jf6);
		jp2.add(jp3_jf7);
		jp3_jf1 = SwingElement.getJT("", 15, 120, 230, 25);
		jp3_jf2 = SwingElement.getJT("", 15, 145, 230, 25);
		jp3_jf3 = SwingElement.getJT("", 15, 170, 230, 25);
		jp3_jf4 = SwingElement.getJT("", 15, 195, 230, 25);
		jp3_jf5 = SwingElement.getJT("", 15, 220, 230, 25);

		// 单选按钮区
		jp3_jrb1 = new JRadioButton("入口页面点(即左侧S1设置的网址页面)", true);
		jp3_jrb1.setBounds(3, 58, 270, 20);

		jp3_jrb2 = new JRadioButton("单以下内页随机取一个（入口页面可以找到该链接）");
		jp3_jrb2.setBounds(3, 98, 325, 20);
		// jp3_bg1.add(jp3_jrb1);
		// jp3_bg1.add(jp3_jrb2);

		// jp3.add(jp3_jf1);
		// jp3.add(jp3_jf2);
		// jp3.add(jp3_jf3);
		// jp3.add(jp3_jf4);
		// jp3.add(jp3_jf5);
		// jp3.add(jp3_jrb2);
		// jp3.add(jp3_jrb1);
		// jp3.add(jp3_jf7);
		// jp3.add(jp3_jf6);
		// jp3.add(jp3_j1);
		// jp3.add(jp3_j2);
		// jp3.add(jp3_j3);
		// jp3.add(jp3_j4);
		// c.add(jp3);

		jp4 = SwingElement.getJP("");
		jp4.setLayout(null);
		jp4.setBounds(330, 300, 329, 300);

		jp4_j1 = SwingElement.getJL("广告联盟:");
		jp4_j1.setBounds(2, 2, 80, 20);

		jp4_j2 = SwingElement.getJL("商业推广链接点击个数：");
		jp4_j2.setBounds(2, 190, 145, 20);
		jp2.add(jp4_j2);

		jp4_j5 = SwingElement.getJL("搜索结果快照点击个数：");
		jp4_j5.setBounds(2, 200, 145, 20);
		//jp2.add(jp4_j5);

		jp4_j7 = SwingElement.getJL("浏览器关闭前停留时间：");
		jp4_j7.setBounds(2, 220, 145, 20);
		jp2.add(jp4_j7);

		jp4_jf1 = SwingElement.getJT("1", 147, 190, 20, 20);
		jp2.add(jp4_jf1);

		jp4_jf3 = SwingElement.getJT("0", 147, 200, 20, 20);
//		jp2.add(jp4_jf3);

		jp4_jf5 = SwingElement.getJT("60", 147, 220, 20, 20);
		jp2.add(jp4_jf5);

		jp4_jf2 = SwingElement.getJT("3", 188, 200, 20, 20);
//		jp2.add(jp4_jf2);

		jp4_jf4 = SwingElement.getJT("3", 188, 190, 20, 20);
		jp2.add(jp4_jf4);

		jp4_jf6 = SwingElement.getJT("70", 188, 220, 20, 20);
		jp2.add(jp4_jf6);

		jp4_j3 = SwingElement.getJL("~");
		jp4_j3.setBounds(168, 190, 10, 20);
		jp2.add(jp4_j3);

		jp4_j3 = SwingElement.getJL("~");
		jp4_j3.setBounds(168, 200, 10, 20);
//		jp2.add(jp4_j3);

		jp4_j6 = SwingElement.getJL("~");
		jp4_j6.setBounds(168, 220, 10, 20);
		jp2.add(jp4_j6);

		jp4_j4 = SwingElement.getJL("(随机)");
		jp4_j4.setBounds(208, 190, 40, 20);
		jp2.add(jp4_j4);

		jp4_j6 = SwingElement.getJL("(随机)");
		jp4_j6.setBounds(208, 200, 40, 20);
//		jp2.add(jp4_j6);

		jp4_j8 = SwingElement.getJL("(秒)");
		jp4_j8.setBounds(208, 220, 40, 20);
		jp2.add(jp4_j8);

		jp4_jrb1 = new JRadioButton("360新代码");
		jp4_jrb1.setBounds(200, 3, 90, 20);
		jp4_jrb1.setFont(new Font("微软雅黑", 1, 13));

		jp4_jcb1 = new JCheckBox("开启选词功能(不兼容所有广告块,要用课咨询)");
		jp4_jcb1.setBounds(3, 50, 300, 20);
		jp4_jcb1.setFont(new Font("微软雅黑", 1, 13));

		jp4_jb1 = SwingElement.getJB("词库凌");
		jp4_jb1.setBounds(15, 70, 75, 20);
		jp4_jb2 = SwingElement.getJB("词库早");
		jp4_jb2.setBounds(110, 70, 75, 20);
		jp4_jb3 = SwingElement.getJB("词库晚");
		jp4_jb3.setBounds(205, 70, 75, 20);
		jp4_jb4 = SwingElement.getJB("词库中");
		jp4_jb4.setBounds(110, 93, 75, 20);

		jp4_jb5 = SwingElement.getJB("任务传入");
		jp4_jb5.addActionListener(new Lister());
		jp4_jb5.setActionCommand("任务传入");
		jp4_jb5.setBounds(20, 240, 110, 25);
		jp2.add(jp4_jb5);

		// jp4.add(jp4_jb4);
		// jp4.add(jp4_jb3);
		// jp4.add(jp4_jb2);
		// jp4.add(jp4_jb1);
		// jp4.add(jp4_jcb1);
		// jp4.add(jp4_jrb1);
		// jp4.add(jp4_j1);

		// c.add(jp4);
	}

	private String ActionScroller() {
		// TODO Auto-generated method stub
		return null;
	}

	public class Lister implements ActionListener {

		String year = jf1.getText();// 年份
		String MM = jf2.getText();// 月
		String dd = jf3.getText();// 日
		String HH = jf4.getText();// 时
		String ss = jf4.getText();// 分
		String yourURL = jp2_jf1.getText();// 你的网址
		String rondomBefore = jp2_jf2.getText();// 随机打开前那个
		String rondomAfter = jp2_jf3.getText();// 随机打开后面那个
		String subpageBefore = jp3_jf1.getText();// 随机打开子页数前那个
		String subpageAfter = jp3_jf2.getText();// 随机打开子页数后
		String URL1 = jp3_jf3.getText();// 5个网址
		String URL2 = jp3_jf4.getText();
		String URL3 = jp3_jf5.getText();
		String URL4 = jp3_jf6.getText();
		String URL5 = jp3_jf7.getText();
		String generalizeBefore = jp4_jf1.getText();// 商业推广前
		String generalizeAfter = jp4_jf2.getText();// 后
		String snapshootBefore = jp4_jf3.getText();// 搜索快照前
		String snapshootAfter = jp4_jf4.getText();// 后
		String closeBefore = jp4_jf5.getText();// 浏览器关闭前
		String closeAfter = jp4_jf6.getText();// 后

		// 传入任务的调用方法
		@Override
		public void actionPerformed(ActionEvent e) {
			int count=0;
			year = jf1.getText();
			MM = jf2.getText();
			dd = jf3.getText();
			HH = jf4.getText();
			ss = jf5.getText();
			yourURL = jp2_jf1.getText();
			rondomBefore = jp2_jf2.getText();
			rondomAfter = jp2_jf3.getText();
			subpageBefore = jp3_jf6.getText();
			subpageAfter = jp3_jf7.getText();
			URL1 = jp3_jf1.getText();
			URL2 = jp3_jf2.getText();
			URL3 = jp3_jf3.getText();
			URL4 = jp3_jf4.getText();
			URL5 = jp3_jf5.getText();
			generalizeBefore = jp4_jf1.getText();
			generalizeAfter = jp4_jf4.getText();
			snapshootBefore = jp4_jf3.getText();
			snapshootAfter = jp4_jf2.getText();
			closeBefore = jp4_jf5.getText();
			closeAfter = jp4_jf6.getText();
			SqlSession session = GetSqlSession.getSession();
			TaskListDao dao = session.getMapper(TaskListDao.class);
			TaskList s = new TaskList();

			// s.setTimeOptions(String.valueOf(getGeneralize()));待定
			if(yourURL.length()!=0&&yourURL.indexOf("http")!=0){
				yourURL="http://"+yourURL;
			}
			s.setYourWebsite(yourURL);
			s.setSwitchingIP(jrb7.isSelected() ? "不切换ip" : "vpn");
			s.setExecutingState("未执行");
			s.setArrivalMode("360进入");
			if (jp2_jrb3.isSelected()) {
				s.setArrivalMode("百度进入");
			}
			// 判断时间
			Date date = new Date(year + "/" + MM + "/" + dd + " " + HH + ":"
					+ ss);
			if (date.getTime() < System.currentTimeMillis()
					&& jrb1.isSelected()) {
				date=new Date();
			}
			// 判断是否明天执行
			if (jrb4.isSelected()) {
				date = new Date(date.getTime() + 60 * 60 * 24 * 1000);
			}
			s.setTimer(date.getTime());
			s.setPointAdsPage(yourURL);
			//当前user.getUserpassword()为远端user的ID
			s.setUserId(user.getId());
			if (jp3_jrb2.isSelected()) {
				String[] urls = { URL1, URL2, URL3, URL4, URL5 };
				s.setPointAdsPage(urls[new Random().nextInt(5)]);
			}

			WebsiteDao webDao = session.getMapper(WebsiteDao.class);
			LexiconDao lexDao = session.getMapper(LexiconDao.class);
			List<Website> webList = webDao.selectAll();
			List<Lexicon> lexList = lexDao.selectAll();
			session.close();
			session = GetSqlSession.getSession();
			dao = session.getMapper(TaskListDao.class);
			// 判断是否选中 单时间任务
			if (jrb1.isSelected()) {
				s.setTaskType(1);
				SqlSession ses=GetSqlSession.getSession();
				TaskListDao taskDao = ses.getMapper(TaskListDao.class);
				if(taskDao.selectByTaskType(2)!=0){
					JOptionPane.showMessageDialog(null, "已存在多时间任务,不得传入单时间任务");
					return;
				}
				ses.close();
				// 判断用户是否输入网址
				if (yourURL.length() == 0) {
					for (int i = 0; i < webList.size(); i++) {
						if(webList.get(i).getWebsite().indexOf("http")!=0){
							webList.get(i).setWebsite("http://"+webList.get(i).getWebsite());
						}
						s.setYourWebsite(webList.get(i).getWebsite());
						if (jp2_jc1.isSelected()) {// 是否改词
							//for (int j = 0; j < lexList.size(); j++) {
//							s.setAdvertisingOptions(lexList.get(j)
//									.getWords());// 设置词,一对多
							s.setAdvertisingOptions(lexList.get(i%lexList.size())
									.getWords());// 设置词一对一
								setRandom(s);
								dao.insert(s);
								count++;
							//}
						} else {
							setRandom(s);
							dao.insert(s);
							count++;
						}
					}
				} else {
					setRandom(s);
					dao.insert(s);
					count++;
				}
			} else {
				s.setTaskType(2);
				SqlSession ses=GetSqlSession.getSession();
				TaskListDao taskDao = ses.getMapper(TaskListDao.class);
				if(taskDao.selectByTaskType(1)!=0){
					JOptionPane.showMessageDialog(null, "已存在单时间任务,不得传入多时间任务");
					return;
				}
				ses.close();
				// 判断初始时间和结束时间
				Calendar c = Calendar.getInstance();
				c.setTime(datewbk);
				Calendar c2 = Calendar.getInstance();
				c2.setTime(date2wbk);
				//是否+1天执行
				if (jrb4.isSelected()) {
					c.add(Calendar.DATE, 1);
					c2.add(Calendar.DATE, 1);
				}
				//设置安排天数
				int dateJf12=Integer.parseInt(jf12.getText());
				//计划安排天数
				for(int  ha=0; ha<dateJf12; ha++){
				try {
					// 设置时间
					int hours=Integer.parseInt(jf6.getText());
					c.set(Calendar.HOUR_OF_DAY,hours );
					c.set(Calendar.MINUTE, Integer.parseInt(jf7.getText()));
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(null, "开始时间输入异常");
					return;
				}
				//判断开始时间是否小于当前时间
//				if(new Date().getTime()>c.getTime().getTime()+ha*1000*3600*24){
//					c.setTime(new Date(System.currentTimeMillis()+ha*1000*3600*24));
//				}
				try {
					int hours=Integer.parseInt(jf8.getText());
					c2.set(Calendar.HOUR_OF_DAY, hours);
					c2.set(Calendar.MINUTE, Integer.parseInt(jf9.getText()));
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(null, "结束时间输入异常");
					return;
				}
				System.out.println(c.getTime().toLocaleString());
				System.out.println(c2.getTime().toLocaleString());
				
				
				
//				if(c2.getTimeInMillis()<System.currentTimeMillis()){
//					JOptionPane.showMessageDialog(null, "结束时间小于当前时间");
//					return;
//				}
				//判断结束时间是否小于开始时间
				
				if (c.getTimeInMillis() > c2.getTimeInMillis()) {
					c2.add(Calendar.DATE, 1);
				}
				int sjsj = 0;// 随机分钟
				int gdsj = 0;// 固定随机分钟
				try {
					sjsj = Integer.parseInt(jf11.getText());
					gdsj = Integer.parseInt(jf10.getText());
					//固定 时间判断是否小于2 
					if(gdsj<2) gdsj=2;
				} catch (Exception e2) {
					JOptionPane.showMessageDialog(null, "随机时间输入异常");
					return;
				}
				
					boolean wflag = true;
					while (wflag) {
						if (yourURL.length() == 0) {
							for (int i = 0; i < webList.size(); i++) {
								if(webList.get(i).getWebsite().indexOf("http")!=0){
									webList.get(i).setWebsite("http://"+webList.get(i).getWebsite());
								}
								s.setYourWebsite(webList.get(i).getWebsite());
								if (jp2_jc1.isSelected()) {
//									for (int j = 0; j < lexList.size(); j++) {
//									s.setAdvertisingOptions(lexList.get(j)
//									.getWords());// 设置词,一对多
							s.setAdvertisingOptions(lexList.get(i%lexList.size())
									.getWords());// 设置词一对一
										s.setTimer(c.getTime().getTime());
										setRandom(s);
										dao.insert(s);
										count++;
										c.add(Calendar.SECOND, new Random().nextInt(sjsj*60) + gdsj*60);
										wflag = c2.getTimeInMillis() > c.getTimeInMillis() ? true : false;
										if (!wflag) {
											break;
										}
//									}
								} else {
									s.setTimer(c.getTime().getTime());
									setRandom(s);
									dao.insert(s);
									count++;
									c.add(Calendar.SECOND,
											new Random().nextInt(sjsj*60) + gdsj*60);
									wflag = c2.getTimeInMillis() > c
											.getTimeInMillis() ? true : false;
									if (!wflag) {
										break;
									}
								}
							}
							if (!wflag) {
								break;
							}
						} else {
							s.setTimer(c.getTime().getTime());
							setRandom(s);
							dao.insert(s);
							count++;
							c.add(Calendar.SECOND, new Random().nextInt(sjsj*60) + gdsj*60);
							wflag = c2.getTimeInMillis() > c.getTimeInMillis() ? true : false;
						}
					}
					c.add(Calendar.DATE, 1);
					c2.add(Calendar.DATE, 1);
				}
			}
			session.close();
			// 刷新任务列表
			Index.refresh(Index.table, user.getId());
			LogUtil.logInsert(LogUtil.GREEN, "成功添加"+count+"条任务!");
			// 杀掉本窗口
			TackTo.this.dispose();
		}

		public int getSubpage() {
			try {
				return new Random().nextInt(Integer.parseInt(subpageAfter)
						- Integer.parseInt(subpageBefore) + 1)
						+ Integer.parseInt(subpageBefore);
			} catch (Exception e) {
				return 0;
			}

		}

		public int getStoptime() {
			try {
				return new Random().nextInt(Integer.parseInt(rondomAfter)
						- Integer.parseInt(rondomBefore) + 1)
						+ Integer.parseInt(rondomBefore);
			} catch (Exception e) {
				return 0;
			}

		}

		// 搜索快照
		public int getSnapshoot() {
			try {
				return new Random().nextInt(Integer.parseInt(snapshootAfter)
						- Integer.parseInt(snapshootBefore) + 1)
						+ Integer.parseInt(snapshootBefore);
			} catch (Exception e) {
				return 0;
			}

		}

		// 关闭前停留
		public int getClose() {
			try {
				return new Random().nextInt(Integer.parseInt(closeAfter)
						- Integer.parseInt(closeBefore) + 1)
						+ Integer.parseInt(closeBefore);
			} catch (Exception e) {
				return 0;
			}

		}

		public int getGeneralize() {
			try {
				return new Random().nextInt(Integer.parseInt(generalizeAfter)
						- Integer.parseInt(generalizeBefore) + 1)
						+ Integer.parseInt(generalizeBefore);
			} catch (Exception e) {
				return 0;
			}

		}
		public void setRandom(TaskList s) {
			s.setSnapshotClick(String
					.valueOf(getSnapshoot()));
			s.setPromotionClick(String
					.valueOf(getGeneralize()));
			s.setPV(getSubpage());
			s.setStopBeforeTime(String
					.valueOf(getClose()));
			s.setRollStopTime(String
					.valueOf(getStoptime()));
		}
	}

	public static String txt2String(File file) {
		StringBuilder result = new StringBuilder();
		BufferedReader br = null;
		
		try {
			if (IPAddress.codeString(file).equalsIgnoreCase("GBK")) {
				System.out.println("GBK");
				br = new BufferedReader(new UnicodeReader(new FileInputStream(file), "GBK"));
			} else {
				System.out.println("UTF-8");
				br = new BufferedReader(new UnicodeReader(new FileInputStream(file),"UTF-8"));// 构造一个BufferedReader类来读取文件
			}
			/*br = new BufferedReader(new InputStreamReader(
					new FileInputStream(file)));*/
			/*System.out.println(Charset.defaultCharset().name());*/
			// br = new BufferedReader(new UnicodeReader(new FileInputStream(file),Charset.defaultCharset().name()));  
			String s = null;
			while ((s = br.readLine()) != null) {// 使用readLine方法，一次读一行
				result.append(s + ",");
			}
			
			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result.toString();
	}
	
}